package com.nova.bluetooth.ui.bluetoothDemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.nova.bluetooth.R;
import com.nova.bluetooth.adapter.AddDeviceConfigAdapter;
import com.nova.bluetooth.base.BaseActivity;
import com.nova.bluetooth.bean.DeviceConfigBean;
import com.nova.bluetooth.bean.SupportFunctionBean;
import com.nova.bluetooth.databinding.ActivityConfigFunctionBinding;
import com.nova.bluetooth.manage.MMKVManager;
import com.nova.bluetooth.helpUtils.SanagDeviceConfigMessage;
import com.nova.bluetooth.helpUtils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class ConfigFunctionActivity extends BaseActivity {

    private AddDeviceConfigAdapter mAdapter;
    private ActivityConfigFunctionBinding binding;

    public static Intent newIntent(AppCompatActivity activity) {
        return new Intent(activity, ConfigFunctionActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityConfigFunctionBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
        initData();
        setPaddingBottom(this,binding.getRoot());
    }

    private void initView() {
        mAdapter = new AddDeviceConfigAdapter();
        RecyclerView recyclerView = findViewById(R.id.recy_add_config);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.radio_option1) {
                    SupportFunctionBean supportFunctionBean = mAdapter.getItem(position);
                    supportFunctionBean.setCheck(!supportFunctionBean.isCheck());
                    mAdapter.getData().set(position, supportFunctionBean);
                }
            }
        });

        binding.ivBack.setOnClickListener(v -> finish());
        binding.ivCleanText.setOnClickListener(v -> binding.etInputName.setText(""));
        binding.btnSave.setOnClickListener(v -> saveData());

        binding.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_option1) {
                    deviceType = SanagDeviceConfigMessage.DEVICE_TYPE_TWS;
                } else if (checkedId == R.id.radio_option2) {
                    deviceType = SanagDeviceConfigMessage.DEVICE_TYPE_HEADSET;
                } else if (checkedId == R.id.radio_option3) {
                    deviceType = SanagDeviceConfigMessage.DEVICE_TYPE_BOX;
                }
                switchDeviceTypeAdapterData(deviceType);
            }
        });
    }


    private void initData() {
        switchDeviceTypeAdapterData(SanagDeviceConfigMessage.DEVICE_TYPE_TWS);
    }

    private void switchDeviceTypeAdapterData(int deviceType) {
        switch (deviceType) {
            case SanagDeviceConfigMessage.DEVICE_TYPE_TWS:
                mAdapter.setList(SanagDeviceConfigMessage.getTwsSupportFunctionList());
                break;
            case SanagDeviceConfigMessage.DEVICE_TYPE_HEADSET:
                mAdapter.setList(SanagDeviceConfigMessage.getHeadsetSupportFunctionList());
                break;
            case SanagDeviceConfigMessage.DEVICE_TYPE_BOX:
                mAdapter.setList(SanagDeviceConfigMessage.getBoxSupportFunctionList());
                break;
        }
    }

    private int deviceType = SanagDeviceConfigMessage.DEVICE_TYPE_TWS;

    private void saveData() {
        if (binding.etInputName.getText().toString().isEmpty()) {
            ToastUtils.show("BLE命名为空");
            return;
        }


        List<DeviceConfigBean> historyDeviceConfigBeanList = getExistConfigData();
        DeviceConfigBean deviceConfigBean = new DeviceConfigBean(
                binding.etInputName.getText().toString(),
                deviceType,
                getSupportFunctionList(mAdapter.getData()));
        historyDeviceConfigBeanList.add(deviceConfigBean);
        MMKVManager.saveDeviceConfigBeanList(historyDeviceConfigBeanList);
        ToastUtils.show("配置信息已保存");


        // 创建一个 Intent 来存储返回的数据
        Intent resultIntent = new Intent();
        // 设置数据，键为 "key"
        resultIntent.putExtra("key", "配置信息已保存，请更新列表显示");
        // 设置结果码和包含数据的 Intent
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
    }

    /**
     * 获取选中的功能
     *
     * @param adapterData
     * @return
     */
    private List<Integer> getSupportFunctionList(List<SupportFunctionBean> adapterData) {
        List<Integer> list = new ArrayList<>();
        for (SupportFunctionBean supportFunctionBean : adapterData) {
            if (supportFunctionBean.isCheck()) {
                list.add(supportFunctionBean.getSupportId());
            }
        }
        Log.d("ConfigFunctionActivity", "getSupportFunctionList size : " + list.size());
        return list;
    };

    private List<DeviceConfigBean> getExistConfigData() {
        if (MMKVManager.getDeviceConfigBeanList() == null) {
            return new ArrayList<>();
        }
        return MMKVManager.getDeviceConfigBeanList();
    }


    /**
     * 判断输入字符串是否符合匹配规则
     *
     * @param input 输入字符串
     * @return 如果输入字符串符合匹配规则，返回 true；否则返回 false
     */
    public boolean isValidString(String input) {
        if (input == null) {
            return false;
        }

        if (input.contains("sanag") ||
                input.contains("Sanag") ||
                input.contains("塞那") ||
                input.contains("Pro") ||
                input.contains("Max") ||
                input.contains("for") ||
                input.contains("APP") ||
                input.contains("AI")){
            return true;
        }
        return false;
    }
}